﻿using Datav.Design.Service;
using Zdap.Common.AutoMapper;
using Zdap.XCode.Service;

namespace Datav.Design.IService
{
    /// <summary>
    ///
    /// </summary>
    public class BaseComService : IBaseComService
    {

        /// <summary>
        /// 
        /// </summary>
        /// <param name="treeNodes"></param>
        /// <param name="parentId"></param>
        /// <returns></returns>
        public async ValueTask<IEnumerable<ComsModelDto>> BulidTreeByRecursive(IEnumerable<ComsModelDto> treeNodes, long parentId)
        {
            var resps = new List<ComsModelDto>();
            var tempList = treeNodes.Where(c => c.ParentId == parentId).OrderBy(s => s.Sort).ToList();

            foreach (var item in tempList)
            {
                ComsModelDto node = item.MapTo<ComsModelDto>();
                node.Children = await BulidTreeByRecursive(treeNodes, node.Id);
                resps.Add(node);
            }
            return await Task.FromResult(!resps.Any() && parentId != 0 ? [] : resps);
        }
    }
}